package cn.zhj.springboot.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import cn.zhj.springboot.domain.User;
import cn.zhj.springboot.exception.R;
import cn.zhj.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
//@SaCheckLogin
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取全部用户信息
     *
     * @return
     */
    @GetMapping("/getUsers")
    @SaCheckRole("admin")
    public R<List<User>> getUsers() {
        List<User> users = userService.getUsers();
        return R.OK(users);
    }

    @PostMapping("/transfer")
//    @SaCheckLogin
    public R transfer(Long payerId, Long payeeId, Double money) {
        return userService.transfer(payerId, payeeId, money);
    }

    @GetMapping("/getUserById/{id}")
//    @SaCheckRole("admin")
    public R<User> getUserById(@PathVariable("id") Long id) {
        return R.OK(userService.getUserById(id));
    }

    // 测试登录
    @PostMapping("/doLogin")
//    @SaIgnore
    public R doLogin(String userName, String password) {
        return userService.doLogin(userName, password);
    }

    // 测试注销
    @GetMapping("/logout")
    @SaIgnore
    public R logout() {
        StpUtil.logout();
        return R.OK();
    }
}
